// fenxiao/rechargeOther/index.js
const app = getApp();
const utils = require('../../utils/util.js');
const util = require('../../utils/pay.js');
// const pay = require('../../merchant/utils/pay.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    rechargeId: '',
    rechargeInfo: null,
    money:'',
    isAgree: false,
    rulesStatus: true,
    isJiuCard: 0,//0普通的余额充值1玖玖卡充值
    mobile: ''
  },

  onLoad: function(options){
    if(options.isJiuCard){
      this.setData({
        isJiuCard: options.isJiuCard
      })
      // this.data.isJiuCard = options.isJiuCard
    }
  },

  getbindinput: function (e) {
    this.setData({
      rechargeId: e.detail.value
    })
  },

  bindMobile: function(e){
    this.setData({
      mobile: e.detail.value
    })
  },

  getbindinputmoney: function (e) {
    this.setData({
      money: parseInt(e.detail.value)
    })
  },
  agree: function(){
    this.setData({
      isAgree: !this.data.isAgree
    })
  },
  rulesStatus: function(){
    this.setData({
      rulesStatus: !this.data.rulesStatus
    })
  },

  //获取绑定好友信息
  getRechargeInfo: function () {
    let that = this;
    if (!that.data.rechargeId) {
      wx.showModal({
        title: '提示',
        content: '请输入代充值用户ID',
        showCancel: false
      })
      return;
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + that.data.rechargeId,
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          if (res.data.Data) {
            let rechargeInfo = res.data.Data;
            rechargeInfo.Avater = utils.formatAvater(rechargeInfo.Avater, 'https://static.711688.net.cn/upload/card/source/');
            that.setData({
              rechargeInfo: rechargeInfo
            })
          } else {
            wx.showModal({
              title: '提示',
              content: "该成员还未创建玖玖客！",
              showCancel: false
            })
            that.setData({
              rechargeId: ''
            });
            return
          }
        }
      },
    })
  },
  //金额验证
  checkMoney: function (obj) {
    if (!obj) {
      return false;
    } else {
      return typeof obj === 'number' && obj % 50 === 0;//是整数，则返回true，否则返回false
    }
  },

  submitOrder: function(){
    if(this.data.isJiuCard == 0){//余额充值
      this.submitAccountBalanceOrder();
    }
    if(this.data.isJiuCard == 1){//玖玖卡充值
      this.submitBalanceOrder();
    }
  },

  submitBalanceOrder: function(){
    let that = this;
    if (!that.checkMoney(that.data.money)) {
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
    wx.showLoading({
      title: '正在充值',
      mask: true
    })
    
    wx.request({
      url: app.globalData.host + '/api/order/SubmitBalanceOrder',
      data: {
        uid: app.globalData.userInfo.Uid,
        count: that.data.money/50,
        mobile: that.data.mobile
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          util.jiujiupay(app, res.data.Data.Oid, '/merchant/merchants/jiuCardDetail/index?cid=that.data.carditemid', app.globalData.userInfo.Uid);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 代充值（余额）
  submitAccountBalanceOrder: function(){
    let that = this;
    if(!that.data.rechargeId){
      wx.showModal({
        title: '提示',
        content: '请输入代充值用户ID',
        showCancel: false,
      })
      return
    }
    if (!that.checkMoney(that.data.money)) {
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
    wx.showLoading({
      title: '正在充值',
      mask: true
    })
    wx.request({
      url: app.globalData.host + "/api/Order/SubmitAccountBalanceOrder",
      data: {
        uid: that.data.rechargeId,
        count: that.data.money/50,
        orderUid: app.globalData.userInfo.Uid
      },
      method: 'POST',
      success: function(res){
        wx.hideLoading();
        if(res.data.Code == 0){
          util.wxpay(app, res.data.Data, 1);
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel:false
          })
        }
      }
    })
  }
})